﻿restore_holy_roman_empire_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_dynasty_house.dds"
	}
	decision_group_type = major

	ai_check_interval = 12

	desc = restore_holy_roman_empire_decision_desc
	selection_tooltip = restore_holy_roman_empire_decision_tooltip

	is_shown = {
		is_landed_or_landless_administrative = yes
		exists = dynasty # Everyone should have a dynasty, but in case they don't...
		NOT = { has_global_variable = flag_restored_roman_empire } # Not if Rome already exists (prevent Rome from restoring HRE)
		NOR = {	#Title existence cnditions.
			exists = title:e_hre.holder	#Well, that'd be redundant.
			exists = title:e_france.holder	#And that'd just be weird.
			highest_held_title_tier = tier_empire	#Existing emperors likewise should not be giving up their empire.
		}
		AND = {	#Faith conditions.
			NOT = { this = faith.religious_head }	#Nice try, Mr. Pope.
			faith = {
				religion_tag = christianity_religion
				has_doctrine_parameter = spiritual_head_of_faith	#Gotta be invested with the authority by someone other than yourself.
			}
			NAND = {	#No need for a competing empire if your faith controls the ERE.
				exists = title:e_byzantium.holder
				faith = title:e_byzantium.holder.faith
			}
		}
		OR = {	#Government conditions.
			government_has_flag = government_is_feudal
			government_has_flag = government_is_clan
			government_allows = administrative
		}
		exists = faith.religious_head
	}

	is_valid = {
		#Standard requirements.
		is_independent_ruler = yes
		trigger_if = {
			limit = {
				is_ai = no
			}
			prestige_level >= high_prestige_level
		}
		#Title ownership conditions.
		AND = {
			has_title = title:k_italy
			OR = { #Gotta be King of a core Carolingian Kingdom.
				has_title = title:k_east_francia
				has_title = title:k_bavaria
				has_title = title:k_france
				has_title = title:k_lotharingia
				any_vassal = {
					OR = {
						has_title = title:k_east_francia
						has_title = title:k_bavaria
						has_title = title:k_france
						has_title = title:k_italy
						has_title = title:k_lotharingia
					}
				}
			}
			custom_description = {
				any_held_title = {
					tier = tier_kingdom
					has_title_law = feudal_elective_succession_law
				}
				text = has_convene_diet_loc
			}
		}
		trigger_if = {
			limit = {
				is_ai = no
			}
			custom_description = {	#And have some additional royal dignity for good measure.
				text = decision_refound_hre_own_three_kingdoms.tt
				any_held_title = {
					tier = tier_kingdom
					count >= 3
				}
			}
		}
		sub_realm_size >= 120	#Plus your various kingdoms should be ruling something.
		#HoF preference conditions.
		trigger_if = {
			limit = {
				is_ai = no
			}
			OR = {
				faith.religious_head = {	#And they have to actually like you enough to want to invest you.
					opinion = {
						target = root
						value >= high_positive_opinion
					}
				}
				root = { has_strong_usable_hook = faith.religious_head }	#Or you have a strong hook on them that you can actually use.
				root = { has_weak_hook = faith.religious_head }	#Or a regular hook, to be fair, provided you meet the other criteria.
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		faith.religious_head = { is_available_adult = yes }
		NOR = {
			has_trait = excommunicated
			is_at_war_with = faith.religious_head
		}
	}

	effect = {
		show_as_tooltip = {
			restore_holy_roman_empire_decision_scripted_effect = yes
		}
		gain_heroic_legend_seed_tooltip_effect = yes
		save_scope_as = founder
		faith.religious_head = { save_scope_as = founder_hof }
		if = {
			limit = {
				has_realm_law = confederate_partition_succession_law
			}
			add_realm_law_skip_effects = partition_succession_law
		}
		if = {
			limit = {
				faith.religious_head = { is_playable_character = yes }
			}
			faith.religious_head.capital_province = { save_scope_as = ceremony_locale }
		}
		else = {
			capital_province = { save_scope_as = ceremony_locale }
		}
		if = {
			limit = {
				faith = faith:catholic
				title:k_romagna.holder = faith.religious_head
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			create_title_and_vassal_change = {
				type = swear_fealty
				save_scope_as = change_two
				add_claim_on_loss = no
			}
			every_held_title = {
				limit = {
					this.kingdom = title:k_romagna
					NOT = {
						this = scope:kinggerman.capital_county
					}
				}
				change_title_holder_include_vassals = {
					holder = scope:founder.faith.religious_head
					change = scope:change
				}
			}
			every_vassal = {
				limit = {
					this.primary_title.kingdom = title:k_romagna
				}
				change_liege = {
					liege = scope:founder.faith.religious_head
					change = scope:change_two
				}
			}
			resolve_title_and_vassal_change = scope:change
			resolve_title_and_vassal_change = scope:change_two
		}
		else = {
			capital_province = { save_scope_as = ceremony_locale }
		}
		trigger_event = {
			id = middle_europe_decisions.0015
		}
		every_held_title = {
			limit = {
				tier = 4
			}
			save_scope_as = destry_this
			root = {
				destroy_title = scope:destry_this
			}
		}
		#Notify other players.
		every_player = {
			limit = {
				NOT = { this = root }
				is_within_diplo_range = { CHARACTER = root }
			}
			trigger_event = middle_europe_decisions.0016
		}
		if = {
			limit = {
				is_ai = no
				NOT = { exists = global_var:restore_holy_roman_empire_decision  }
			}
			set_global_variable = {
				name = restore_holy_roman_empire_decision
				value = root
			}
		}
		# Assign Dynamic Prince-Electors
		hidden_effect = {
			if = {
				limit = { has_global_variable_list = hre_elector_list }
				clear_global_variable_list = hre_elector_list
			}
			while = {
				count = 7
				random_vassal = {
					limit = {
						capital_province.empire = title:e_hre # must be de-jure vassal of empire
						OR = { # prince-bishopric or duchy
							AND = {
								government_has_flag = government_is_theocracy
								primary_title.tier >= tier_county
							}
							primary_title.tier >= tier_duchy
						}
						NOT = { # not already selected
							is_target_in_global_variable_list = {
								name = hre_elector_list
								target = primary_title
							}
						}
					}
					weight = {
						modifier = { # major vassals heavily weighted
							factor = 10
							is_powerful_vassal = yes
						}
						modifier = { # same culture as emperor preferred
							factor = 4
							culture = root.culture
						}
						modifier = { # we don't mind going outside Germania, but prefer it
							factor = 1.5
							trigger = { exists = primary_title.title_province }
							primary_title.title_province.barony = {
								OR = {
									target_is_de_jure_liege_or_above = title:k_bavaria
									target_is_de_jure_liege_or_above = title:k_bohemia
									target_is_de_jure_liege_or_above = title:k_east_francia
									target_is_de_jure_liege_or_above = title:k_frisia
									target_is_de_jure_liege_or_above = title:k_lotharingia
									target_is_de_jure_liege_or_above = title:k_pomerania
								}
							}
						}
						modifier = { # we want at least 3 theocracies ideally
							factor = 10
							any_in_global_list = {
								variable = hre_elector_list
								count < 3
								holder = { government_has_flag = government_is_theocracy }
							}
							government_has_flag = government_is_theocracy
						}
						modifier = { # 3 is enough theocracies unless we can't find feudal
							factor = 0.1
							any_in_global_list = {
								variable = hre_elector_list
								count >= 3
								holder = { government_has_flag = government_is_theocracy }
							}
							government_has_flag = government_is_theocracy
						}
					}
					add_to_global_variable_list = {
						name = hre_elector_list
						target = primary_title
					}
				}
			}
			hre_elector_list_save_effect = yes # saves list scopes to title as variables for tooltip
		}
	}

	cost = {
		gold = {
			if = {
				limit = {
					is_ai = no
				}
				add = 500
			}
			else = {
				add = 200
			}
		}
		piety = 200
	}

	ai_potential = {
		is_independent_ruler = yes
		highest_held_title_tier >= tier_kingdom
	}

	ai_will_do = {
		base = 100

		modifier = { # We don't want a solo-France to form the HRE
			factor = 0
			NOR = {
				has_title = title:k_east_francia
				has_title = title:k_bavaria
				has_title = title:k_italy
				has_title = title:k_lotharingia
				any_vassal = {
					OR = {
						has_title = title:k_east_francia
						has_title = title:k_bavaria
						has_title = title:k_italy
						has_title = title:k_lotharingia
					}
				}
			}
		}
	}
}